
package com.idealighter.game.dictionary.dic;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.SharkIconRateDomain;
import com.idealighter.game.dictionary.mapper.SharkIconRateMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Singleton
public class SharkIconRateDic implements DictionaryTable<SharkIconRateDomain> {

  private SharkIconRateMapper mapper;
  private List<SharkIconRateDomain> list;
  private Map<Integer, SharkIconRateDomain> map;

  // row:图片方案id,column:图标id
  private Table<Integer, Integer, SharkIconRateDomain> iconRates;

  @Inject
  public SharkIconRateDic(SharkIconRateMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<SharkIconRateDomain> list() {

    return list;
  }

  @Override
  public Map<Integer, SharkIconRateDomain> map() {

    return map;
  }

  @Override
  public void load() {
    this.list = mapper.selectAll();
    Map<Integer, SharkIconRateDomain> map = new HashMap<>();
    Table<Integer, Integer, SharkIconRateDomain> iconRates = HashBasedTable.create();
    list.forEach(val -> {
      map.put(val.getIcon(), val);
      iconRates.put(val.getPlan(), val.getIcon(), val);
    });
    this.iconRates = iconRates;
    this.map = map;
  }

  @Override
  public SharkIconRateDomain get(int icon) {
    return map.get(icon);
  }

  public Table<Integer, Integer, SharkIconRateDomain> getIconRates() {
    return iconRates;
  }

  /**
   * 获取权值总值.
   * 
   * @Title totalProbability.
   * @author houdongsheng
   * @date 2018年1月16日 上午11:14:57
   */
  public int totalProbability(Map<Integer, SharkIconRateDomain> iconRates) {
    int result = 0;
    if (iconRates != null && !iconRates.isEmpty()) {
      for (SharkIconRateDomain iconRateDom : iconRates.values()) {
        result += iconRateDom.getProbability();
      }
    }
    return result;
  }
}
